<?php

/**
 * Project: CrazyPoems
 * Author: Alexandru
 * Date: 20/10/2008
 * File: login.php
 */

// Load default settings.
require('inc/config.php');
require('inc/functions.misc.php');
require('inc/functions.index.php');
require('inc/functions.login.php');

// Redirect if already logged in.
if (isset($cookie->username) && strlen($cookie->username) > 0) {
	header('Location: '.BASE_URL);
}

$smarty->assign('pageTitle', 'Login');
$smarty->assign('breadcrumbs', '<a href="'.BASE_URL.'">'.l('Inicio').'</a> &raquo; '.l('Entrar'));

// Define variables.
$error_msg = "";
$username = isset($_POST['username']) && strlen($_POST['username']) > 0 ? $_POST['username'] : '';
$password = isset($_POST['password']) && strlen($_POST['password']) > 0 ? $_POST['password'] : '';

// Validate data.
if (isset($_POST['form_sent']) && $_POST['form_sent'] == "yes") {
	if (strlen($username) == 0) {
		$error_msg .= "<p>".l('Introducir Usuario')."</p>";
	}
	if (strlen($password) == 0) {
		$error_msg .= "<p>".l('Introducir Contraseña')."</p>";
	}
	// Login.
	if (strlen($error_msg) == 0) {
		Hop_login($username, $password);
	}
}

// Smarty.
$smarty->assign('form_sent', $_POST['form_sent']);
$smarty->assign('error_msg', $error_msg);
$smarty->assign('username', $username);
$smarty->assign('full_url', get_full_url());

// Go!
$smarty->display('login.tpl');

// End page.
require('inc/end.php');
?>
